package com.junzhiit.nengji.cms.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.ChainInfoService;
import com.junzhiit.nengji.cms.web.exception.ChainInfoNotFoundException;
import com.junzhiit.nengji.cms.web.exception.ChainNotFoundException;
import com.junzhiit.nengji.cms.web.util.aliyun.AliyunOSSClientUtil;
import com.junzhiit.nengji.cms.web.vo.ChainInfoVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.ChainDAO;
import com.junzhiit.nengji.dao.ChainInfoDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Chain;
import com.junzhiit.nengji.model.ChainInfo;

@Service
public class ChainInfoServiceImpl implements ChainInfoService {

	@Autowired
	SystemUserCacheDAO systemUserCacheDAO;
	@Autowired
	ChainDAO chainDAO;
	@Autowired
	ChainInfoDAO chainInfoDAO;
	@Value("${upload.mall.chain.info}")
	private String chainInfoPath;
	
	@Value("${upload.temp}")
	private String uploadPath;
	
	//阿里云API的bucket名称
	@Value("${oss.backet.name}")
	private String backetName;

	@Override
	public ResultVO findChainInfoBySessionId(String sessionId) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO session = systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId),
				SystemUserSessionVO.class);
		Chain chain = chainDAO.query(session.getSystemId());
		if (chain == null) {
			throw new ChainNotFoundException("品牌未找到");
		}
		String chainId=chain.getId();
		ChainInfo chainInfo =chainInfoDAO.findByChainId(chainId);
		ChainInfo newChainInfo=new ChainInfo();
		if (chainInfo == null) {
			newChainInfo.setChainId(chainId);
			chainInfoDAO.insert(newChainInfo);
		}else{
			BeanUtils.copyProperties(chainInfo, newChainInfo);
		}
		result.setData(newChainInfo);
		return result;
	}

	@Override
	public ResultVO updateChainInfo(ChainInfoVO chainInfoVO) {
		ResultVO result = new ResultVO();
		ChainInfo chainInfo=chainInfoDAO.query(chainInfoVO.getId());
		if(chainInfo==null){
			throw new ChainInfoNotFoundException("品牌商业信息未找到");
		}
		BeanUtils.copyProperties(chainInfoVO, chainInfo);
		chainInfoDAO.update(chainInfo);
		return result;
	}

	@Override
	public ResultVO delImg(Integer imgType, String chainInfoId) {
		ResultVO result = new ResultVO();
		ChainInfo chainInfo = chainInfoDAO.query(chainInfoId);
		switch(imgType){
			case 0:
				//FileUtil.removeFile(chainInfoPath+chainInfo.getIdentityFrontImgUrl());
				AliyunOSSClientUtil.removeFile(backetName, chainInfoPath+chainInfo.getIdentityFrontImgUrl());
				chainInfo.setIdentityFrontImgUrl(null);
				chainInfoDAO.update(chainInfo,true);
				break;
			case 1:
				//FileUtil.removeFile(chainInfoPath+chainInfo.getIdentityBackImgUrl());
				AliyunOSSClientUtil.removeFile(backetName, chainInfoPath+chainInfo.getIdentityBackImgUrl());
				chainInfo.setIdentityBackImgUrl(null);
				chainInfoDAO.update(chainInfo,true);
				break;
			case 2:
				//FileUtil.removeFile(chainInfoPath+chainInfo.getIdentityImgUrl());
				AliyunOSSClientUtil.removeFile(backetName, chainInfoPath+chainInfo.getIdentityImgUrl());
				chainInfo.setIdentityImgUrl(null);
				chainInfoDAO.update(chainInfo,true);
				break;
			case 3:
				//FileUtil.removeFile(chainInfoPath+chainInfo.getSubjectImgUrl());
				AliyunOSSClientUtil.removeFile(backetName, chainInfoPath+chainInfo.getSubjectImgUrl());
				chainInfo.setSubjectImgUrl(null);
				chainInfoDAO.update(chainInfo,true);
				break;
	}
		return result;
	}

}
